import sqlite3
import urllib3
import os

db=sqlite3.connect(os.path.dirname(__file__)+"/test.db")
cur = db.cursor()
def getquestion(id):
    http = urllib3.PoolManager()
    r = http.request(
        'GET',
        'http://xf.faxuan.net/ess/service/getexerinfo?questionId=%d'%(id)
    )
    lis=r.data
    str=lis.decode("utf-8")
    if(str.strip("\n") == "{}"):
        print(id,'false')
        return False
    str=str.replace("[","")
    str = str.replace("\\", "")
    str = str.replace("\"\"", "")
    str = str.replace("]", "")
    str = str.replace("}", "")
    str = str.replace("{", "")
    lis=str.split(",\"")
    dat=[]
    for liss in lis:
        da=liss.split(":")
        #print(da[1].replace("\"\"", "").strip("\""))
        dat.append(da[1].replace("\"\"", "").strip("\""))
    print(dat)

    resulte=cur.execute("select * from xf where id=%s"%(dat[0]))
    if(resulte.fetchone() == None):
        sql_insert = "insert into xf(id,type,content,options) values('%s','%s','%s','%s')"%(dat[0],dat[1],dat[2].strip("\n"),dat[3].strip("\n"))
        print(sql_insert)
        db.execute(sql_insert)
        db.commit()
    else:
        sql = "UPDATE xf SET type=%s,content='%s',options='%s' where (id=%s)" % (
         dat[1], dat[2].strip("\n"), dat[3].strip("\n"),dat[0])
        print(sql)
        db.execute(sql)
        db.commit()

for i in range(300000,305000):
    getquestion(i)

db.close()